<script type="text/html" template>
  <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
  <div class="layadmin-user-login-main">
    <div class="layadmin-user-login-box layadmin-user-login-header">
      <script type="text/html" template>
        <h2>管理员密码重置</h2>
        <p>如果您的账号未关联邮箱，请联系管理员进行重置</p>
      </script>
    </div>
    <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-email"></label>
        <input type="text" name="email" lay-verify="required" placeholder="用户邮箱"
               class="layui-input">
      </div>
      <div class="layui-form-item">
        <div class="layui-row">
          <div class="layui-col-xs7">
            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"></label>
            <input type="hidden" name="key" value=""/>
            <input type="text" name="captcha" placeholder="图形验证码" class="layui-input">
          </div>
          <div class="layui-col-xs5">
            <div style="margin-left: 10px;">
              <img id="captcha" src="" class="layadmin-user-login-codeimg">
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-row">
          <div class="layui-col-xs7">
            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"></label>
            <input type="text" name="verify_code" lay-verify="required" placeholder="验证码"
                   class="layui-input">
          </div>
          <div class="layui-col-xs5">
            <div style="margin-left: 10px;">
              <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid" id="btn_send_code">
                获取验证码
              </button>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password"></label>
        <input type="password" name="new_password" lay-verify="required" placeholder="请输入新密码"
               class="layui-input">
      </div>
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password"></label>
        <input type="password" name="repass" lay-verify="required" placeholder="请再次输入新密码"
               class="layui-input">
      </div>
      <div class="layui-form-item">
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="btn_reset">重置密码</button>
      </div>
    </div>
  </div>
</div>

<script>
  layui.use([], function () {
    var $ = layui.$
      , admin = layui.admin
      , form = layui.form

    form.render();

    loadCaptcha();
    $('#captcha').click(function () {
      loadCaptcha()
    })

    function loadCaptcha() {
      //加载验证码
      admin.get('lay-admin/captcha', function (res) {
        $('input[name="key"]').val(res.data.key)
        $('#captcha').attr('src', res.data.img)
      })
    }

    //发送邮箱验证码
    $('#btn_send_code').click(function () {
      let emailElem = $('input[name="email"]');
      if (!emailElem.val()) {
        layer.tips('请输入邮箱地址', emailElem);
        return;
      }
      let captchaElem = $('input[name="captcha"]');
      let keyElem = $('input[name="key"]');
      if (!captchaElem.val()) {
        layer.tips('请输入图形验证码', captchaElem);
        return;
      }
      admin.post('/lay-admin/email_code', {
        email: emailElem.val(),
        key: keyElem.val(),
        captcha: captchaElem.val(),
      }, function () {
        layer.msg('发送成功', {
          offset: '15px'
          , icon: 1
          , time: 1000
        });
      })
    })

    //提交
    form.on('submit(btn_reset)', function (obj) {
      var field = obj.field;

      //确认密码
      if (field.new_password !== field.repass) {
        return layer.msg('两次密码输入不一致');
      }

      //请求接口
      admin.post('/lay-admin/reset_password', field, function () {
        layer.msg('密码重置成功', {
          offset: '15px'
          , icon: 1
          , time: 1000
        }, function () {
          location.hash = '/user/login'; //跳转到登入页
        });
      })

      return false;
    });
  })
  ;
</script>
